/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl;

import bl.SalaryReportBO;
import common.Common;
import dal.LaborDA;
import entities.Labor;
import java.sql.ResultSet;
import model.CommonTableModel;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Sony
 */
public class SalaryReportPanel extends javax.swing.JPanel {

    /**
     * Creates new form SalaryReportPanel
     */
    SalaryReportBO salaryReportBO = new SalaryReportBO();
    LaborDA laborDA = new LaborDA();
    CommonTableModel model = null;
    Common com = new Common();

    public SalaryReportPanel() {
        initComponents();


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblFrom = new javax.swing.JLabel();
        btnView = new javax.swing.JButton();
        jspSalaryReport = new javax.swing.JScrollPane();
        tblSalaryReport = new javax.swing.JTable();
        txtDateFrom = new com.toedter.calendar.JDateChooser();
        lblTo = new javax.swing.JLabel();
        txtDateTo = new com.toedter.calendar.JDateChooser();

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "SALARY REPORT", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 12))); // NOI18N

        lblFrom.setText("From:");

        btnView.setText("View");
        btnView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewActionPerformed(evt);
            }
        });

        tblSalaryReport.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblSalaryReport.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblSalaryReportMouseClicked(evt);
            }
        });
        jspSalaryReport.setViewportView(tblSalaryReport);

        lblTo.setText("To:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblFrom, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtDateFrom, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblTo, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtDateTo, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnView)
                .addContainerGap(154, Short.MAX_VALUE))
            .addComponent(jspSalaryReport)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtDateFrom, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtDateTo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblFrom, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblTo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(btnView))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jspSalaryReport, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewActionPerformed
        // TODO add your handling code here:
        Date utilDateFrom = txtDateFrom.getDate();
        Date utilDateTo = txtDateTo.getDate();
        if (utilDateFrom == null && utilDateTo == null) {
            JOptionPane.showMessageDialog(this, "Please insert From date and To date");
        } else if (utilDateFrom == null) {
            JOptionPane.showMessageDialog(this, "Please insert From date");
        } else if (utilDateTo == null) {
            JOptionPane.showMessageDialog(this, "Please insert To date");
        }
        if (utilDateFrom != null && utilDateTo != null) {
            if (compareDate(utilDateFrom, utilDateTo) == true) {

                java.sql.Date dateFrom = com.convertDateUtilToSql(utilDateFrom);
                java.sql.Date dateTo = com.convertDateUtilToSql(utilDateTo);

                ResultSet rs = salaryReportBO.showSalary(dateFrom, dateTo);
                model = new CommonTableModel(rs);
                tblSalaryReport.setModel(model);

            } else {
                JOptionPane.showMessageDialog(this, "From date can not be smaller than To date.");
            }
        }
    }//GEN-LAST:event_btnViewActionPerformed

    private void tblSalaryReportMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblSalaryReportMouseClicked
        // TODO add your handling code here:

        java.sql.Date dateFrom = new java.sql.Date(txtDateFrom.getDate().getTime());
        java.sql.Date dateTo = new java.sql.Date(txtDateTo.getDate().getTime());
        int index = tblSalaryReport.getSelectedRow();
        Object obj = tblSalaryReport.getValueAt(index, 0);
        Labor laborSearch = new Labor();
        laborSearch.setLaborID(obj.toString());
        Labor labor = laborDA.getLaborByID(laborSearch.getLaborID());
        LaborDetailDialog ldd = new LaborDetailDialog(null, true);
        ldd.setValueDialog(labor, dateFrom, dateTo);
        ldd.setVisible(true);


    }//GEN-LAST:event_tblSalaryReportMouseClicked

    public boolean compareDate(Date beforDate, Date afterDate) {
        if (beforDate.equals(afterDate)) {
            return false;
        } else if (beforDate.before(afterDate)) {
            return true;
        } else {
            return false;
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnView;
    private javax.swing.JScrollPane jspSalaryReport;
    private javax.swing.JLabel lblFrom;
    private javax.swing.JLabel lblTo;
    private javax.swing.JTable tblSalaryReport;
    private com.toedter.calendar.JDateChooser txtDateFrom;
    private com.toedter.calendar.JDateChooser txtDateTo;
    // End of variables declaration//GEN-END:variables
}
